<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="code" label="全宗号">
        <el-input v-model="dataForm.code" placeholder="请输入全宗号"></el-input>
      </el-form-item>
      <el-form-item prop="name" label="全宗名称">
        <el-input v-model="dataForm.name" placeholder="请输入全宗名称"></el-input>
      </el-form-item>
      <el-form-item prop="tenantCode" label="所属组织机构" class="role-list">
        <el-select v-model="dataForm.tenantCode" placeholder="请选择所属组织机构">
          <el-option v-for="tenant in tenantList" :key="tenant.id" :label="tenant.tenantName" :value="tenant.tenantCode"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="ruleArchiveId" label="档号规则" class="role-list">
        <el-select v-model="dataForm.ruleArchiveId" placeholder="请选择所属档号规则">
          <el-option v-for="ruleSys in ruleList" :key="ruleSys.id" :label="ruleSys.ruleName" :value="ruleSys.archiveId"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils"; // isEmail, isMobile, isNumber,
import { IObject } from "@/types/interface";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      roleList: [] as IObject,
      tenantList: [] as IObject[],
      ruleList: [] as IObject[],
      dataForm: {
        id: "",
        code: "",
        name: "",
        tenantCode: "",
        ruleArchiveId: ""
      }
    });
  },
  computed: {
    dataRule() {
      return {
        code: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        name: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        tenantCode: [{ required: true, message: this.$t("validate.required"), trigger: "change" }]
      };
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        this.getInfo();
        this.getOrgInfo();
        this.getRuleList();
      });
    },
    getRuleList() {
      baseService.get(`/archive/rule/list`).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.ruleList = res.data;
        console.log(res);
      });
    },
    getInfo() {
      if (this.dataForm.id != "") {
        baseService.get(`/archive/info/${this.dataForm.id}`).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        });
      }
    },
    getOrgInfo() {
      baseService.get(`/sys/tenant/listAll`).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.tenantList = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/archive/info", {
          ...this.dataForm
        }).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>

<style lang="less">
.mod-tenant__tenant {
  .role-list {
    .el-select {
      width: 100%;
    }
  }
}
</style>
